<?php

namespace app\api\controller;

use app\api\controller\merchant\Publics;
use app\api\controller\resigter\Bases;
use app\common\controller\Api;
use app\common\func\Tools;
use fast\Random;

/**
 * 注册
 * Class Register
 * @package app\api\controller
 *
 * @method mixed check_machine()  			检测设备是否绑定
 * @method mixed register()					注册【商户 | 客户】
 * @method mixed customer_binds_wx()		客服绑定微信
 */
class Register extends Api
{
	protected $noNeedLogin = ['register', 'check_machine', 'customer_binds_wx'];
	protected $noNeedRight = '*';

	/**
	 * 检测设备是否绑定
	 */
	public function check_machine()
	{
		$mcode = $this->request->post("mcode");
		if (!$mcode) {
			$this->error(__('参数错误'));
		}
		$check = Bases::instance()->check_machine_bind($mcode);
		if ($check) {
			$info = Publics::instance()->info_mmcode($mcode);
			if ($info) {
				$this->success("正常.....", ["id" => $info["id"], "nick" => $info["nickname"]]);
			}
		}
		$this->error("机器未绑定");
	}

	/**
	 * 注册【商户 | 客户】
	 */
	public function register()
	{
		$nickname = $this->request->post("names");
		$phone = $this->request->post("phone");
		$machine = $this->request->post("mcode");
		$openid = $this->request->post("openid");
		$userid = $this->request->post("userid", 0);
		$province = $this->request->post("province");
		$city = $this->request->post("city");
		$district = $this->request->post("district");
		$address = $this->request->post("address");
		$merchant_id = $this->request->post("merchant_id", 0);
		$product_id = $this->request->post("product_id", 0);
		$method = $this->request->post("type", 0);

		$card_front = $this->request->post("card_front", '');
		$card_back  = $this->request->post("card_back", '');
		$license 	= $this->request->post("license", '');
		$real_name 	= $this->request->post("real_name", '');
		$real_idcard = $this->request->post("real_idcard", '');

		// 类型 1 商户 2 客户
		$typs = $this->request->post("typ");

		if (!in_array($typs, [1, 2])) {
			$this->error("注册类型错误");
		}

		if (!$nickname && !$phone && !$machine && !$openid && !$typs) {
			$this->error(__('参数错误'));
		}


		$prams = [
			"nickname" 	 	=> $nickname,
			"username" 	 	=> $phone,
			"mobile"   	 	=> $phone,
			"wx_open_id" 	=> $openid,
			"mer_id"	 	=> $merchant_id,
			"mercode"	 	=> $machine,
			"is_typs"  	 	=> $typs,
			"pid" 		 	=> $userid
		];
		if ($typs == 1) {
			$prams["idcards"] = $real_idcard;
			$prams["is_verify"] = 1;
			$prams["salt"]		= Random::alnum();
			$prams["password"]  = $this->auth->getEncryptPassword($phone, $prams["salt"]);
		}

		$imgPrams = [
			"card_front" 	=> $card_front,
			"card_back"  	=> $card_back,
			"license"	 	=> $license,
			"real_name"	 	=> $real_name,
			"real_idcard" 	=> $real_idcard
		];

		if (!$province && !$city && !$district && !$address) {
			$this->error("地址参数错误");
		}
		$addrsPrams = [
			"name" 			=> $nickname,
			"mobile"		=> $phone,
			"province"		=> $province,
			"city"			=> $city,
			"district"		=> $district,
			"address"		=> $address,
		];

		if ($typs == 2) {
			//if (!$product_id && !$method) {
			if (!$method) {
				$this->error("申领参数错误");
			}
		}
		// 查看注册电话唯一
		$check = Bases::instance()->check_phone_unique($phone, $typs);
		if ($check) {
			// 注册 - 添加商户|客户
			$req = Bases::instance()->create_user($prams, $addrsPrams);
			if ($req) {
				if ($typs == 1) {
					$imgPrams["sale_id"] = $req["id"];
					$req = Bases::instance()->merchant_idCard_save($imgPrams);
				}
				if ($typs == 2) {
					// 添加免申领权限 默认 可领取
					\app\data\controller\Apply::instance()->store_auth($req["id"]);
					// 收货地址
					$id = Bases::instance()->address_id($req["id"]);
					// 默认申领
					$ret = \app\common\func\Tools::saveProductApply($product_id, $method, $req["id"], $id);
					if ($ret) {
						// 申领订单记录 - 修改权限
						\app\data\controller\Apply::instance()->place_update_status($req["id"], $ret);
					}
					// 流失客户统计 默认添加
					\app\data\controller\Binds::instance()->customer_lost($req["id"]);
				}
				$this->success(__('注册成功'), $req);
			}
			$this->error("注册失败");
		} else {
			$this->error("手机号已注册");
		}
	}

	/**
	 * 客服绑定微信
	 */
	public function customer_binds_wx()
	{

		$openid = $this->request->post("openid");
		$aid = $this->request->post("aid");
		$gid = $this->request->post("gid", 0);

		if (!$openid && !$gid && !$aid) {
			$this->error(__('参数错误'));
		}
		$prams = [
			"wx_open_id" => $openid,
			"is_typs"  	=> Tools::checkRole($gid),
			"group_id" 	=> $gid,
			"admin_id" => $aid
		];

		$check = Bases::instance()->check_customer_bind_wx($aid, $gid);
		if (!$check) {
			$req = Bases::instance()->create_customer($prams);
			if ($req) {
				$this->success(__('绑定成功'), $req);
			}
			$this->error("注册失败");
		} else {
			$this->error("已绑定");
		}
	}
}
